import java.io.*;
import java.util.StringTokenizer;

/**
 * User: Aleksey
 * Date: 17.02.2009
 * Time: 23:18:44
 */
public class check9 {
    private static int getSum(int[] d, int[][] m, int n) {
        int dist = 0;
        for (int i = 1; i < n; i++) {
            dist += (m[d[i-1]][d[i]]);
        }
        dist += m[d[n-1]][d[0]];
        return dist;
    }

    public static void main(String[] args) {
        File inputFile = new File(args[0]);
        File userOutputFile = new File(args[1]);
        //File templateOutputFile = new File(args[2]);

        int result = 0;
        try {
            BufferedReader br1 = new BufferedReader(new FileReader(userOutputFile));
            BufferedReader br2 = new BufferedReader(new FileReader(inputFile));

            int n = Integer.parseInt(br2.readLine().trim());
            int[][] m = new int[n][n];

            for (int i = 0; i < n; i++) {
                StringTokenizer st = new StringTokenizer(br2.readLine());
                for (int j = 0; j < n; j++) {
                    m[i][j] = Integer.parseInt(st.nextToken());
                }
            }

            int[] d1 = new int[n];
            StringTokenizer st = new StringTokenizer(br1.readLine());
            for (int j = 0; j < n; j++) {
                d1[j] = Integer.parseInt(st.nextToken());
            }

            int[] d = new int[n];
            boolean[] used = new boolean[n];

            d[0] = 0;
            used[0] = true;
            for (int i = 1; i < n; i++) {
                int best = -1;
                for (int j = 0; j < n; j++) {
                    if (!used[j] && (best == -1 || m[d[i-1]][j] < m[d[i-1]][best])) {
                        best = j;
                    }
                }
                d[i] = best;
                used[best] = true;
            }

            for (int i = 0; i < n; i++) {
                if (d1[i] != d[i]) {
                    System.exit(1);
                }
            }

            String line = br1.readLine();
            while (!line.equals("optimal")) {
                st = new StringTokenizer(line);
                int first = Integer.parseInt(st.nextToken());
                int second = Integer.parseInt(st.nextToken());
                int sum_before = getSum(d, m, n);
                int work = d[first];
                d[first] = d[second];
                d[second] = work;
                int sum_after = getSum(d, m, n);
                if (sum_after >= sum_before) {
                    System.exit(1);
                }
                line = br1.readLine();
            }
            Boolean ok = true;

            for (int i = 1; i < n; i++) {
                for (int j = i+1; j < n; j++) {
                    if (j-i > 1) {
                        int dBefore = m[d[i-1]][d[i]] + m[d[i]][d[i == n-1 ? 0 : i+1]] + m[d[j-1]][d[j]] + m[d[j]][d[j == n-1 ? 0 : j+1]];
                        int dAfter = m[d[i-1]][d[j]] + m[d[j]][d[i == n-1 ? 0 : i+1]] + m[d[j-1]][d[i]] + m[d[i]][d[j == n-1 ? 0 : j+1]];
                        if (dAfter < dBefore) {
                            ok = false;
                        }
                    }
                    else {
                        int dBefore = m[d[i-1]][d[i]] + m[d[i]][d[j]] + m[d[j]][d[j == n-1 ? 0 : j+1]];
                        int dAfter = m[d[i-1]][d[j]] + m[d[j]][d[i]] + m[d[i]][d[j == n-1 ? 0 : j+1]];
                        if (dAfter < dBefore) {
                            ok = false;
                        }
                    }
                }
            }

            if (!ok) {
                System.exit(1);
            }

            br1.close();
            br2.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        System.exit(result);
    }
}